<template>
  <view class="login">
    <view class="content">
      <!-- 头部logo -->
      <view class="header">
        <image :src="logoImage" />
      </view>
      <!-- 主体表单 -->
      <view class="main">
        <wInput v-model="username" type="text" maxlength="11" placeholder="用户名/手机号"></wInput>
        <wInput v-model="password" type="password" maxlength="11" placeholder="密码"></wInput>
      </view>
      <wButton text="登 录" :rotate="isRotate" @click.native="startLogin()" class="wbutton"></wButton>

      <!-- 其他登录 -->
      <view class="other_login cuIcon">
        <view class="login_icon">
          <!-- <view class="cuIcon-weixin" @tap="login_weixin"></view> -->
        </view>
        <!-- <view class="login_icon">
          <view class="cuIcon-weibo" @tap="login_weibo"></view>
        </view>-->
        <!-- <view class="login_icon">
          <view class="cuIcon-github" @tap="login_github"></view>
        </view>-->
      </view>

      <!-- 底部信息 -->
      <view class="footer">
        <!-- <navigator url="forget" open-type="navigate">找回密码</navigator>
        <text>|</text>
        <navigator url="register" open-type="navigate">注册账号</navigator> -->
      </view>
    </view>
  </view>
</template>

<script>
import wInput from "../../components/watch-login/watch-input.vue"; //input
import wButton from "../../components/watch-login/watch-button.vue"; //button
import { mapActions } from "vuex";

import { checkExpire } from "../../utils/auth.js";
import { logoImage } from "./common";
export default {
  data() {
    return {
      //logo图片 base64
      logoImage: logoImage,
      // username: "yxlo", //用户/电话
      // password: "123456789", //密码
      username: "", //用户/电话
      password: "", //密码
      isRotate: false, //是否加载旋转
      gotoUrl: "",
      switchUrl: "index"
    };
  },
  components: {
    wInput,
    wButton
  },
  onLoad() {
    this.isLogin();
  },
  methods: {
    ...mapActions({
      login: "login",
      initSocket: "initSocket",
      getMainShop: "getMainShop",
      getShops: "getShops"
    }),
    isLogin() {
      // 检查
      // checkExpire(async res => {
      //   if (res) {
      //     // 直接跳转到相应页面
      //     if (this.gotoUrl) {
      //       this.$goto(this.gotoUrl);
      //     } else {
      //       uni.switchTab({
      //         url: `/pages/${this.switchUrl}/${this.switchUrl}`
      //       });
      //     }
      //   }
      // });
    },
    startLogin() {
      // 登录
      if (this.isRotate) {
        // 判断是否加载中，避免重复点击请求
        return false;
      }
      if (this.username.length == "") {
        uni.showToast({
          icon: "none",
          position: "bottom",
          title: "用户名不能为空"
        });
        return;
      }
      if (this.password.length < 5) {
        uni.showToast({
          icon: "none",
          position: "bottom",
          title: "密码不正确"
        });
        return;
      }
      this.isRotate = true;

      this.login({
        username: this.username,
        password: this.password
      })
        .then(async res => {
          await this.getMainShop();
          await this.getShops();
          // await this.initSocket();
          uni.switchTab({
            url: `/pages/${this.switchUrl}/${this.switchUrl}`
          });
        })
        .catch(err => {
          uni.showToast({
            icon: "none",
            title: err.errMsg
          });
        })
        .finally(async () => {
          this.isRotate = false;
        });
    },
    login_weixin() {
      // 微信登录
      uni.showToast({
        icon: "none",
        position: "bottom",
        title: "微信登录"
      });
    },
    login_weibo() {
      // 微博登录
      uni.showToast({
        icon: "none",
        position: "bottom",
        title: "微博登录"
      });
    },
    login_github() {
      //github登录
      uni.showToast({
        icon: "none",
        position: "bottom",
        title: "github"
      });
    }
  }
};
</script>

<style>
@import url("../../components/watch-login/css/icon.css");
@import url("./css/main.css");
</style>
